/**
 * 
 */
package kr.co.insoft.core.repository.configration.cdt.impl;

import java.util.List;

import kr.co.insoft.core.mapper.configration.cdt.CDTMapper;
import kr.co.insoft.core.model.configration.cdt.CDTEntityModel;
import kr.co.insoft.core.model.configration.cdt.CDTListParam;
import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.repository.configration.cdt.CDTRepository;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
@Repository
public class CDTRepositoryImpl implements CDTRepository {

	private static final Logger CDTRepositoryImplLog = LoggerFactory
			.getLogger(CDTRepositoryImpl.class);

	@Autowired
	CDTMapper cdtMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.setting.usertype.UserTypeRepository#getLists
	 * (kr.co.insoft.core.model.paging.PagingModel)
	 */
	@Override
	public List<CDTEntityModel> getLists(PagingModel pagingModel)
			throws RepositoryException {
		try {
			Assert.notNull(pagingModel);
			Assert.notNull(pagingModel.getCurrentPage());
		} catch (IllegalArgumentException e) {
			CDTRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, pagingModel);
			CDTRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, pagingModel.getCurrentPage());
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return cdtMapper.getLists(pagingModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see kr.co.insoft.core.repository.setting.usertype.UserTypeRepository#
	 * getTotalCount(kr.co.insoft.core.model.paging.PagingModel)
	 */
	@Override
	public int getTotalCount(PagingModel pagingModel)
			throws RepositoryException {
		try {
			Assert.notNull(pagingModel);
		} catch (IllegalArgumentException e) {
			CDTRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, pagingModel);
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return cdtMapper.getTotalCount(pagingModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see kr.co.insoft.core.repository.setting.usertype.UserTypeRepository#
	 * createUserType
	 * (kr.co.insoft.core.model.setting.usertype.UserTypeEntityModel)
	 */
	@Override
	public void createCDT(CDTEntityModel cdtEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(cdtEntityModel);
			Assert.notNull(cdtEntityModel.getUserTypeName());
		} catch (IllegalArgumentException e) {
			CDTRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, cdtEntityModel);
			CDTRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, cdtEntityModel.getUserTypeName());
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			cdtEntityModel.setUserTypeId(cdtMapper.getUuid());
			cdtMapper.createCDT(cdtEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.setting.usertype.UserTypeRepository#getUserType
	 * (kr.co.insoft.core.model.setting.usertype.UserTypeEntityModel)
	 */
	@Override
	public CDTEntityModel getCDT(CDTEntityModel cdtEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(cdtEntityModel);
			Assert.notNull(cdtEntityModel.getUserTypeId());
		} catch (IllegalArgumentException e) {
			CDTRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, cdtEntityModel);
			CDTRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, cdtEntityModel.getUserTypeId());
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return cdtMapper.getCDT(cdtEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public void editCDT(CDTEntityModel cdtEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(cdtEntityModel);
			Assert.notNull(cdtEntityModel.getUserTypeId());
		} catch (IllegalArgumentException e) {
			CDTRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, cdtEntityModel);
			CDTRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, cdtEntityModel.getUserTypeId());
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			cdtMapper.editCDT(cdtEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public void removeCDT(CDTEntityModel cdtEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(cdtEntityModel);
			Assert.notNull(cdtEntityModel.getUserTypeId());
		} catch (IllegalArgumentException e) {
			CDTRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, cdtEntityModel);
			CDTRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, cdtEntityModel.getUserTypeId());
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			cdtMapper.removeCDT(cdtEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public List<CDTEntityModel> getCDTs(CDTListParam cdtListParam)
			throws RepositoryException {
		try {
			Assert.notNull(cdtListParam);
			Assert.notNull(cdtListParam.getUserid());
			Assert.notNull(cdtListParam.getCompanyid());
		} catch (IllegalArgumentException e) {
			CDTRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, cdtListParam);
			CDTRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, cdtListParam.getUserid());
			CDTRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT, cdtListParam.getCompanyid());
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return cdtMapper.getCDTs(cdtListParam);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
}
